<?php

/**
 * @Author: wuchenge
 * @Date:   2022-04-08 18:04:01
 * @Email: ligui0506@126.com
 * @Descript:
 */
namespace App\Admin\Controllers;

use App\Http\Controllers\ApiController;
use App\Models\Level;
use App\Requests\LevelRequest;
use Illuminate\Http\Request;

class LevelController extends ApiController
{
    /**
     * @var mixed
     */
    protected $model;

    /**
     * @param Request $request
     */
    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->model   = new Level();
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function all(Request $request)
    {
        $levels = Level::select(['name', 'id'])->get();
        return $this->response($levels);
    }

    /**
     * @param HotRequest $request
     * @return mixed
     */
    public function list(Request $request)
    {
        $keyword = $request->input('keyword', '');
        $filter  = $request->input('filter', '');
        $sort    = $request->input('sort', '');
        $page    = $request->input('page', 1);
        $limit   = $request->input('limit', 10);

        $where = ['nomal' => []];
        if (!empty($keyword)) {
            $like = ['name', 'like', $keyword . '%'];
            array_push($where['nomal'], $like);
        }

        if (!empty($filter)) {
            $kv = explode('-', $filter);
            array_push($where['nomal'], $kv);
        }

        $order = [];
        if (!empty($sort)) {
            $kv            = explode('-', $sort);
            $order[$kv[0]] = $kv[1];
        }
        $field = ['*'];

        $data = $this->model::lists(Level::query(), $field, $where, $page,
            $limit,
            $order);
        return $this->response($data);
    }

    /**
     * @param Request $request
     */
    public function create(LevelRequest $request)
    {
        $res = Level::create([
            'name'       => $request->input('name'),
            'self_tip'   => $request->input('self_tip'),
            'parent_tip' => $request->input('parent_tip'),
            'color'      => $request->input('color'),
        ]);

        if ($res) {
            return $this->response();
        }

        return $this->failed();
    }

    /**
     * @param $id
     * @return mixed
     */
    public function detail(Request $request)
    {
        $level = Level::find($request->input('id'));
        return $this->response($level);
    }

    /**
     * @param LevelRequest $request
     * @param $id
     */
    public function update(LevelRequest $request, $id)
    {
        $data = [
            'name'       => $request->input('name'),
            'self_tip'   => $request->input('self_tip'),
            'parent_tip' => $request->input('parent_tip'),
            'color'      => $request->input('color'),
        ];

        $res = Level::where('id', $id)->update($data);
        if ($res) {
            return $this->response();
        }

        return $this->failed();
    }
}
